package com.lingrit.provider.exception;

import com.lingrit.provider.vto.FpResponse;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author JuiceYan
 * @date 2017/12/1
 */
@ControllerAdvice
public class FpExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(FpExceptionHandler.class);

    @ExceptionHandler(value =Exception.class)
    @ResponseBody
    public FpResponse handleFpException(HttpServletRequest request, FpException excption) {
        FpResponse fpResponse = new FpResponse();
        String message = request.getRemoteHost() + "-" + excption.getMessage();
        ExceptionMsg exceptionMsg = ExceptionMsg.FAILED;
        if (excption instanceof FpException){
            FpException fpException = (FpException)excption;
            exceptionMsg = fpException.getExceptionMsg();
        }

        fpResponse.setCode(exceptionMsg.getCode());
        logger.error("FpExceptionHandler:" + excption.getMessage());
        return fpResponse;
    }
}
